<template>
  <AcceptHandleHeadComponent
    ref="acceptHandleHeadRef"
    :ucolumns="ucolumns"
    :type="LableTitle.accptEnd"
    :needMoreQuery="true"
    @myEvent="myEvent"
    @setColumns="setColumns"
    @onSearch="onSearch"
    @onReset="reQuery"
  >
    <!-- 其他查询条件 -->
    <div slot="more">
      <a-form layout="inline" class="accept_more_operate_moreForm">
        <a-collapse v-model="collapseKey" class="accept_more_operate_moreForm_collapse">
          <a-collapse-panel :show-arrow="false" disabled key="1" style="font-weight: bold">
            <div slot="header" class="accept_more_operate_moreForm_collapse_head">
              <a-row :gutter="48" style="width: 100%">
                <a-col @click.stop="() => {}" :md="8" :sm="24">
                  <a-form-item label="申请人：">
                    <a-input v-model="queryParam.applyname" placeholder="请输入申请人" allowClear />
                  </a-form-item>
                </a-col>
                <a-col @click.stop="() => {}" :md="8" :sm="24">
                  <a-form-item label="被申请人：">
                    <a-input v-model="queryParam.targetname" placeholder="请输入被申请人" allowClear />
                  </a-form-item>
                </a-col>
                <a-col @click.stop="() => {}" :md="8" :sm="24">
                  <a-form-item label="程序类型：">
                    <a-select
                      v-model="queryParam.proctype"
                      style="width: 200px; font-weight: normal"
                      placeholder="请选择程序类型"
                      allowClear
                    >
                      <a-select-option v-for="item in Tp_apptypeList" :key="item.value" :value="item.value">
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
              </a-row>
            </div>
            <a-row :gutter="48" style="width: 100%" class="accept_more_operate_moreForm_collapse_body">
              <a-col @click.stop="() => {}" :md="8" :sm="24">
                <a-form-item label="来源：">
                  <a-select
                    v-model="queryParam.source"
                    style="width: 220px; font-weight: normal"
                    placeholder="请选择来源"
                    allowClear
                  >
                    <a-select-option v-for="item in Tp_applysourceList" :key="item.value" :value="item.value">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col @click.stop="() => {}" :md="12" :sm="24">
                <a-form-item label="申请时间：">
                  <a-date-picker
                    v-model="queryParam.applyStartDate"
                    :style="{ width: 'calc(50% - 10px)' }"
                    placeholder="开始日期"
                    format="YYYY-MM-DD"
                    allowClear
                    @change="subtimeListen()"
                  />
                  ~
                  <a-date-picker
                    v-model="queryParam.applyEndDate"
                    :style="{ width: 'calc(50% - 12px)' }"
                    placeholder="结束日期"
                    format="YYYY-MM-DD"
                    allowClear
                    @change="subtimeListen()"
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
        </a-collapse>
      </a-form>
    </div>

    <template slot="content">
      <div class="accept_content_table">
        <!-- 表格数据 -->
        <!-- :scroll="{ x:1000, y: 550 }" -->
        <a-table
          :scroll="scroll"
          :columns="ucolumns"
          :data-source="listData"
          :pagination="upagination"
          :loading="loading"
          rowKey="taskid"
          @change="tableChange"
          size="middle"
          :bordered="ubordered"
          :rowClassName="rowClassName"
          :components="header"
          ref="table"
          :key="tableCountKey"
        >
          <span slot="serial" slot-scope="text, record, index">
            {{ (upagination.current - 1) * upagination.pageSize + parseInt(index) + 1 }}
          </span>

          <div slot="ismediateSlot" slot-scope="text, record" class="beyond_hiding" style="width: 100%">
            <a-tooltip placement="topLeft">
              <template slot="title">
                <span>{{
                  (record.ismediate || record.ismediate_bs) == 3200
                    ? '是'
                    : (record.ismediate || record.ismediate_bs) == 3201
                    ? '否'
                    : ''
                }}</span>
              </template>
              <span :class="{ accept_content_table_font_red: record.ismediate == 3200 || record.ismediate_bs == 3200 }">
                {{
                  (record.ismediate || record.ismediate_bs) == 3200
                    ? '是'
                    : (record.ismediate || record.ismediate_bs) == 3201
                    ? '否'
                    : ''
                }}
              </span>
            </a-tooltip>
          </div>

          <div slot="statusSlot" slot-scope="text, record" class="beyond_hiding" style="width: 100%">
            {{ record.status_text }}
            <a-popover v-show="record.state == '1'" placement="topLeft">
              <template slot="content">
                <span>{{
                  record.enddate
                    ? `${record.enddate.substring(0, 4)}年${record.enddate
                        .substring(5, 7)
                        .replace(/\b(0+)/gi, '')}月${record.enddate.substring(8, 10).replace(/\b(0+)/gi, '')}日截止
                        ${record && record.leftDay > 0 ? '当前剩余' + record.leftDay + '天工作日' : ''}`
                    : '无截止日期'
                }}</span>
              </template>
            </a-popover>
          </div>

          <div slot="sourceSlot" slot-scope="source_text" class="beyond_hiding" style="width: 100%">
            {{ source_text }}
          </div>

          <div slot="dealNameSlot" slot-scope="text, record" class="beyond_hiding" style="width: 100%">
            <a-tooltip placement="topLeft">
              <template slot="title">
                <span>{{ dealNameLabel(record) }}</span>
              </template>
              {{ dealNameLabel(record) }}
            </a-tooltip>
          </div>

          <div slot="applynameSlot" slot-scope="applyname" class="beyond_hiding" style="width: 100%; text-align: left">
            <a-tooltip placement="topLeft">
              <template slot="title">
                <span>{{ applyname }}</span>
              </template>
              {{ applyname }}
            </a-tooltip>
          </div>

          <div slot="proctypeSlot" slot-scope="text, record" class="overBox" style="width: 100%">
            <a-tooltip placement="topLeft">
              <template slot="title">
                <span>{{ proctypeLabel(record) }}</span>
              </template>
              {{ proctypeLabel(record) }}
            </a-tooltip>
          </div>

          <div slot="subtimeSlot" slot-scope="subtime" class="beyond_hiding" style="width: 100%">
            {{ subtime | getTimeFormat('YYYY年M月D日') }}
          </div>

          <div slot="endtimeSlot" slot-scope="endtime" class="beyond_hiding" style="width: 100%">
            {{ endtime | getTimeFormat('YYYY年M月D日') }}
          </div>

          <div slot="processSlot" slot-scope="process" class="beyond_hiding" style="width: 100%; text-align: left">
            <a-tooltip placement="topLeft">
              <template slot="title">
                <span>{{ process }}</span>
              </template>
              {{ process }}
            </a-tooltip>
          </div>

          <div slot="acceptcodeSlot" slot-scope="text, record" class="beyond_hiding" style="width: 100%">
            <a-tooltip placement="topLeft">
              <template slot="title">
                <span>{{ record.acceptcode }}</span>
              </template>
              {{ record.acceptcode }}
            </a-tooltip>
          </div>

          <div
            slot="targetnameSlot"
            slot-scope="text, record"
            class="beyond_hiding"
            style="width: 100%; text-align: left"
          >
            <a-tooltip placement="topLeft">
              <template slot="title">
                <span>{{ record.targetname_text }}</span>
              </template>
              {{ record.targetname_text }}
            </a-tooltip>
          </div>

          <span slot="operation" slot-scope="text, record">
            <template>
              <a href="javascript:;" @click="showHand(record, '4')">详情</a>
            </template>
          </span>
        </a-table>
      </div>
    </template>
  </AcceptHandleHeadComponent>
</template>

<script>
import drag from '@/components/TableDrag/drag'
import { St_admreview } from '@/enumeration/St_admreview'
import { Tp_applysourceList } from '@/enumeration/Tp_applysource'
import { Tp_apptypeList } from '@/enumeration/Tp_apptype.js'
import { LableTitle } from '@/enumeration/LableTitle'
import { Tp_biztype } from '@/enumeration/Tp_biztype'
import { Tp_admpr } from '@/enumeration/Tp_admpr'
import { queryMf_MyTaskDoneList } from '@/api/workflow/mf_MyTaskDone'
import { APP_USER, APP_DEPT } from '@/store/mutation-types.js'
import storage from 'store'
import AcceptHandleHeadComponent from '@/views/accept/component/AcceptHandleHeadComponent.vue'

const ucolumns = [
  {
    title: '序号',
    width: 50,
    key: 'index',
    align: 'center',
    fixed: 'left',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '状态',
    dataIndex: 'status_text',
    key: 'status_text',
    width: 100,
    align: 'center',
    scopedSlots: { customRender: 'statusSlot' }
  },
  {
    title: '案号',
    dataIndex: 'acceptcode',
    key: 'acceptcode',
    width: 300,
    align: 'center',
    scopedSlots: { customRender: 'acceptcodeSlot' }
  },
  {
    title: '业务名称',
    dataIndex: 'process',
    key: 'process',
    width: 200,
    align: 'center',
    scopedSlots: { customRender: 'processSlot' }
  },
  {
    title: '申请人名称',
    dataIndex: 'applyname',
    key: 'applyname',
    width: 150,
    align: 'center',
    scopedSlots: { customRender: 'applynameSlot' }
  },
  {
    title: '被申请人名称',
    dataIndex: 'targetname_text',
    key: 'targetname_text',
    width: 200,
    align: 'center',
    scopedSlots: { customRender: 'targetnameSlot' }
  },
  {
    title: '是否进行调解',
    dataIndex: 'ismediate_text',
    key: 'ismediate_text',
    width: 120,
    align: 'center',
    scopedSlots: { customRender: 'ismediateSlot' }
  },
  {
    title: '程序类型',
    dataIndex: 'proctype_text',
    key: 'proctype_text',
    width: 100,
    align: 'center',
    scopedSlots: { customRender: 'proctypeSlot' }
  },
  {
    title: '来源',
    dataIndex: 'source_text',
    key: 'source_text',
    width: 100,
    align: 'center',
    scopedSlots: { customRender: 'sourceSlot' }
  },
  {
    title: '承办人',
    width: 100,
    key: 'dealName',
    dataIndex: 'dealName',
    align: 'center',
    scopedSlots: { customRender: 'dealNameSlot' }
  },
  {
    title: '申请时间',
    dataIndex: 'subtime',
    key: 'subtime',
    width: 150,
    align: 'center',
    scopedSlots: { customRender: 'subtimeSlot' }
  },
  {
    title: '处理时间',
    dataIndex: 'endtime',
    key: 'endtime',
    width: 150,
    align: 'center',
    scopedSlots: { customRender: 'endtimeSlot' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    key: 'action',
    width: 220,
    fixed: 'right',
    align: 'center',
    scopedSlots: { customRender: 'operation' }
  }
]

export default {
  data () {
    return {
      ucolumns: ucolumns,
      // columns: columns,
      cardColumns: ucolumns,
      queryParam: {
        acceptcode: '', // 案号
        reguserName: '', // 录入人
        applyname: '', // 申请人
        targetname: '', // 被申请人
        applyStartDate: null, // 开始时间
        applyEndDate: null, // 结束时间
        source: undefined, // 来源
        proctype: undefined, // 程序类型
        pageNumber: 1,
        pageSize: 20,
        order: 'desc',
        userid: '', // 用户id
        state: '1',
        matid: null,
        bizenum: null,
        statusapply: []
      },
      listData: [],
      upagination: {
        // 表格分页参数
        defaultPageSize: 20,
        total: 0,
        current: 1,
        pageSize: 20,
        showTotal: total => `共计 ${total} 条数据`,
        showSizeChanger: true,
        showQuickJumper: true,
        pageSizeOptions: ['10', '20', '50', '100']
      },
      loading: false,
      ubordered: true, // 表格边框是否显示
      mixins: [drag],
      St_admreview: St_admreview,
      Tp_biztype: Tp_biztype,
      Tp_admpr: Tp_admpr,
      currentUser: storage.get(APP_USER), // 登录人信息
      currentDept: storage.get(APP_DEPT), // 登录人部门
      sourceType: [],
      LableTitle: LableTitle,
      header: undefined,
      tableCountKey: 0,
      collapseKey: [1], // 折叠面板key
      Tp_applysourceList: Tp_applysourceList, //
      Tp_apptypeList: Tp_apptypeList,
      scroll: { x: 1000, y: 410 }
    }
  },
  components: {
    AcceptHandleHeadComponent
  },
  computed: {},
  created () {
    let routeParam
    try {
      console.log('this.$route.query.queryParam.queryList', this.$route.query.queryParam.TempqueryParam)
      routeParam = JSON.parse(JSON.stringify(this.$route.query.queryParam.TempqueryParam))
      this.queryParam = { ...this.queryParam, ...this.$route.query.queryParam.TempqueryParam }
    } catch {
      console.log('路由传参非json格式')
    }
    this.headerChange()
    // 待处理人员
    this.initDefaultParam()
    this.queryDoneTaskList()
  },
  mounted () {},
  watch: {
    ucolumns: {
      handler (newValue, oldVal) {
        this.headerChange()
      },
      deep: true // 深度监听
    }
  },
  methods: {
    myEvent (e) {
      if (e) {
        this.scroll.y = this.scroll.y + e
      } else {
        this.scroll.y = 410
      }
    },
    /**
     * 显示处理界面
     */
    showHand (record, pattern) {
      console.log('record', record.taskid)
      record.TempqueryParam = this.queryParam
      this.$router.push({
        path: `/AppeptExamineProcessedOperation/${record.taskid}/${pattern}`,
        query: {
          url: '/AppeptExamineProcessed',
          queryParam: record
        }
      })
    },
    headerChange () {
      console.log('移动后', this.ucolumns)
      this.header = {
        header: {
          cell: drag.methods.initDrag(this.ucolumns)
        }
      }
    },
    setColumns (columns) {
      console.log('重置前', this.ucolumns)
      // tableCountKey自增是为了让表格控件每次销毁强制更新
      this.tableCountKey += 1
      this.ucolumns = columns
      // this.ucolumns = columns
      console.log('重置后', this.ucolumns)
      this.setCardColumns()
    },

    onSearch (val) {
      this.queryParam.acceptcode = val
      this.queryParam.pageNumber = 1
      this.queryParam.pageSize = 20
      this.queryDoneTaskList()
    },

    // 接收时间监听判断开始结束日期是否合理
    subtimeListen () {
      if (
        this.queryParam.applyStartDate > this.queryParam.applyEndDate &&
        this.queryParam.applyStartDate &&
        this.queryParam.applyEndDate
      ) {
        this.queryParam.applyStartDate = undefined
      }
    },
    /**
     * 初始化设置默认值
     */
    initDefaultParam () {
      this.currentUser = storage.get(APP_USER)
      this.queryParam.userid = this.currentUser.userId
      this.queryParam.bizenum = this.Tp_biztype['accept'].value
      this.sourceType = this.Tp_applysourceList
      this.queryParam.matid = '100000_360100-000201009000-QT-002-02'
    },
    /**
     * 重置查询条件并查询数据
     */
    reQuery () {
      this.queryParam = {
        acceptcode: '', // 案号
        reguserName: '', // 录入人
        applyname: '', // 申请人
        targetname: '', // 被申请人
        applyStartDate: null, // 开始时间
        applyEndDate: null, // 结束时间
        source: undefined, // 来源
        proctype: undefined, // 程序类型
        pageNumber: 1,
        pageSize: 20,
        order: 'desc',
        userid: '', // 用户id
        state: '1',
        bizenum: null,
        matid: null,
        statusapply: []
      }
      this.initDefaultParam()
      this.queryDoneTaskList()
    },

    tableChange (upagination, filters, sorter, { currentDataSource }) {
      this.row_idx = upagination.current * upagination.pageSize - upagination.pageSize
      this.queryParam.pageNumber = upagination.current
      this.queryParam.pageSize = upagination.pageSize
      this.queryDoneTaskList()
    },
    // 其他tab页触发修改当前页事件
    tableChangeOther (upagination) {
      this.row_idx = upagination.current * upagination.pageSize - upagination.pageSize
      this.queryParam.pageNumber = upagination.current
      this.queryParam.pageSize = upagination.pageSize
      this.queryDoneTaskList()
    },

    queryDoneTaskList () {
      this.queryList()
    },

    getCpQueryParam () {
      // 时间转换
      const cpQueryParam = JSON.parse(JSON.stringify(this.queryParam))
      if (this.queryParam.applyStartDate?.format) {
        cpQueryParam.applyStartDate = this.queryParam.applyStartDate.format('YYYY-MM-DD') + ' 00:00:00'
      }
      if (this.queryParam.applyEndDate?.format) {
        cpQueryParam.applyEndDate = this.queryParam.applyEndDate.format('YYYY-MM-DD') + ' 23:59:59'
      }
      return cpQueryParam
    },

    queryList () {
      const queryParam = this.getCpQueryParam()
      if (queryParam.acceptcode) {
        queryParam.acceptcode = queryParam.acceptcode.toString().trim()
      }
      if (queryParam.targetname) {
        queryParam.targetname = queryParam.targetname.toString().trim()
      }
      if (queryParam.applyname) {
        queryParam.applyname = queryParam.applyname.toString().trim()
      }
      this.loading = true
      queryMf_MyTaskDoneList(this.currentUser.userId, queryParam)
        .then(res => {
          console.log('表格数据:', res.data)
          if (res.code !== 0) {
            this.$message.error('获取数据失败')
          } else {
            this.listData = res.data.records
            // 将当前页码和数量与后台绑定
            this.upagination.total = res.data.total
            this.upagination.current = res.data.current
            this.upagination.pageSize = res.data.size
            this.needDealNum = res.data.total
            // 对应枚举值设置
            this.setEnumData()
          }
        })
        .catch(err => {
          this.listData = []
          console.log(err, '获取数据失败')
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 设置对应的枚举值
    setEnumData () {
      if (this.listData) {
        this.listData.forEach(item => {
          // 被申请人名称
          item.targetname_text = this.targetNameLable(item)
          // 是否进行调解
          item.ismediate_text =
            (item.ismediate || item.ismediate_bs) == 3200
              ? '是'
              : (item.ismediate || item.ismediate_bs) == 3201
              ? '否'
              : ''
          // 来源
          item.source_text = this.sourceLable(item.source)
          // 状态
          item.status_text = this.statusLable(item)
          // 程序类型
          item.proctype_text = this.proctypeLabel(item)
          // 是否行政不作为
          if (item.isomission_bz == 3200 || item.isomission_bs == 3200) {
            // 行政不作为
            item.prtype_text = '行政不作为'
          } else {
            const prtype = item.prtype_bz || item.prtype_bs
            item.prtype_text = this.Tp_admpr[prtype] ? this.Tp_admpr[prtype].name : ''
          }
          // 超时时间，已完结都为false
          item.isTimeout = false
        })
      }
    },

    // 状态转换显示
    statusLable (record) {
      const val = record.status_bz || record.status_bs
      for (const key in this.St_admreview) {
        if (this.St_admreview[key].value == val) {
          return this.St_admreview[key].name
        }
      }
      return val
    },
    // 来源转换显示
    sourceLable (val) {
      for (const key in this.Tp_applysourceList) {
        if (this.Tp_applysourceList[key].value == val) {
          return this.Tp_applysourceList[key].name
        }
      }
      return val
    },
    /**
     * 程序类型显示
     */
    proctypeLabel (record) {
      const proctype = record.proctype_bz || record.proctype_bs
      const enumItem = this.$store.getters.getEnumItem('Tp_apptype').filter(item => item.value == proctype)[0]
      return enumItem ? enumItem.label : ''
    },
    // 被申请人查询显示
    targetNameLable (record) {
      const val = record.targetname_bz || record.targetname_bs
      return val
    },

    rowClassName (record, index) {
      return index % 2 === 0 ? 'oddRow' : 'evenRow'
    },

    setCardColumns () {
      const columns = this.ucolumns.filter(child => child.key != 'index' && child.key != 'action')
      this.cardColumns = JSON.parse(JSON.stringify(columns))
    },
    /**
     * 案件承办人
     * @param {} record
     */
    dealNameLabel (record) {
      let dealName = record.acceptfirstname_bz || record.acceptfirstname_bs
      if (dealName && (record.acceptsecondname_bz || record.acceptsecondname_bs)) {
        dealName = dealName + ',' + (record.acceptsecondname_bz || record.acceptsecondname_bs)
      }
      return dealName
    }
  }
}
</script>

<style lang="less" scoped>
@import '~@/components/TableDrag/style.less';
</style>

<style lang="scss" scoped>
.accept_content {
  &_table {
    &_font_red {
      color: #ca0000;
      font-size: 16px;
      font-family: 'PingFang-SC-Heavy';
      font-weight: 400;
    }

    ::v-deep .ant-table-row-cell-break-word {
      font-size: 16px;
      font-family: 'PingFang-SC-Heavy';
      font-weight: 400;
      color: #2c2c2c;
    }

    ::v-deep .ant-table-thead > tr > th {
      height: 50px;
    }

    ::v-deep .ant-table-row-cell-break-word {
      font-size: 16px;
      font-family: 'PingFang-SC-Heavy';
      font-weight: 600;
      color: #2c2c2c;
    }

    // ::v-deep .ant-pagination-total-text {
    //   font-size: 16px;
    //   font-family: 'PingFang-SC-Heavy';
    //   font-weight: 400;
    //   color: #8e97b9;
    // }

    ::v-deep .ant-table-body,
    .ant-table-header {
      word-break: break-all;
      &::-webkit-scrollbar {
        width: 10px; /*高宽分别对应横竖滚动条的尺寸*/
        // height: 10px;
      }
      &::-webkit-scrollbar-thumb {
        border-radius: 6px;
        // -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        background: #a2c6e1;
      }
      &::-webkit-scrollbar-track {
        // -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        border-radius: 5px;
        background: transparent;
      }
    }
  }
}
</style>
